import axios from "axios";


export const setWindowFunction = (webview:any)=>{
    (window as any).getWebviewStorage = async ()=>{
      const localStorageData = await webview.executeJavaScript(`
      JSON.stringify(localStorage);`);
      const parsedLocalStorage = JSON.parse(localStorageData);
      return parsedLocalStorage;
    }
    (window as any).axios = axios;
   
    // 注入dom函数
    webview.executeJavaScript(`
    window.f_dom = (xpath) =>{return document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue};
    `);
    //注入wait函数
    webview.executeJavaScript(`
    window.f_wait = (ms) => new Promise(resolve => setTimeout(resolve, ms));
    window.wait = (ms) => new Promise(resolve => setTimeout(resolve, ms));
    `);
    //注入setValue函数
    webview.executeJavaScript(`
    window.f_set = (inputElement,value) =>{
      inputElement.value = value;
      inputElement.classList.add('cls');
      setTimeout(() => {
        inputElement.classList.remove('cls');
        const inputEvent = new Event('input', { bubbles: true, cancelable: true });
        inputElement.dispatchEvent(inputEvent);
        const changeEvent = new Event('change', { bubbles: true, cancelable: true });
        inputElement.dispatchEvent(changeEvent);
      }, 300);
      
    };
    `);
   
    //注入click函数
    webview.executeJavaScript(`
    window.f_click = (inputElement) =>{
      inputElement.classList.add('cls');
      setTimeout(() => {
        inputElement.classList.remove('cls');
        inputElement.click()
      }, 300);
    };
    `);
    //注入push函数
    webview.executeJavaScript(`
    window.f_push = (module) =>{
      if(module.type=='url'){
        window.location = module.url;
      }else{
        f_dom(module.dom).click();
      }
    };
    `);
}



export const setStyle = (webview:any)=>{
  webview.executeJavaScript(`
  const style = document.createElement('style');
  style.innerHTML = ".cls{transform: scale(1.2);}";
  document.head.appendChild(style);

    `);
}